package com.tuanzi.loan.workflow.repository.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.ActHiProcinst.ACT_HI_PROCINST;
import static com.tuanzi.loan.generated.jooq.loan.tables.ActReProcdef.ACT_RE_PROCDEF;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.system.ProcessInstanceSearchRequest;
import com.tuanzi.loan.workflow.repository.ProcessInstanceRepository;
import com.tuanzi.loan.workflow.vo.ProcessInstanceResponse;

/**
 * @author Ray
 * @date 2018/01/12
 */
@Repository
public class ProcessInstanceRepositoryImpl extends JOOQBaseRepository implements ProcessInstanceRepository {

	@Override
	protected TableImpl getTable() {
		return ACT_HI_PROCINST;
	}

	@Override
	public Page<ProcessInstanceResponse> listProcessInstance(ProcessInstanceSearchRequest request, Pageable pageable) {
		innerJoin(ACT_RE_PROCDEF, ACT_HI_PROCINST.PROC_DEF_ID_.eq(ACT_RE_PROCDEF.ID_));
		eq(ACT_RE_PROCDEF.CATEGORY_, request.getCategory());
		return search(pageable, new ProcessInstanceResponseRecordMapper());
	}

	private final class ProcessInstanceResponseRecordMapper implements RecordMapper<Record, ProcessInstanceResponse> {

		@Override
		public ProcessInstanceResponse map(Record record) {
			ProcessInstanceResponse response = new ProcessInstanceResponse();
			response.setId(record.get(ACT_HI_PROCINST.PROC_INST_ID_));
			response.setName(record.get(ACT_HI_PROCINST.NAME_));
			return response;
		}
	}
}
